﻿// <copyright file="ImagePanel.xaml.cs" company="Taasss">Copyright (c) 2009 All Right Reserved</copyright>
// <author>Ben Vincent</author>
// <date>2010-02-21</date>
// <summary>ImagePanel</summary>
namespace Fotofly.PhotoViewer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Threading;

    using Fotofly.PhotoViewer.FotoflyWebService;

    public partial class ImagePanel : UserControl
    {
        private double imageWidth;
        private double imageHeight;
        private Uri imageUri;        
        private double greyThickness = 2;
        private double whiteThickness = 4;
        
        private double totalBorderThickness;

        private List<ImageRegionPanel> regionPanels;

        public ImagePanel(double panelWidth, double panelHeight)
        {
            this.InitializeComponent();

            // Set Border thickness
            this.greyBorder.BorderThickness = new Thickness(this.greyThickness);
            this.whiteBorder.BorderThickness = new Thickness(this.whiteThickness);
            this.totalBorderThickness = ((this.greyThickness + this.whiteThickness) * 2);

            // Save the Width & Height
            this.Width = panelWidth;
            this.Height = panelHeight;

            // Work out the Image Heigth and Width
            this.imageWidth = this.Width - this.totalBorderThickness;
            this.imageHeight = this.Height - this.totalBorderThickness;

            // Set Grey Border Height & Width
            this.greyBorder.Height = this.Height;
            this.greyBorder.Width = this.Width;

            // Set White Border Height & Width
            this.whiteBorder.Height = this.Height - (2 * this.greyThickness);
            this.whiteBorder.Width = this.Width - (2 * this.greyThickness);
        }

        public bool HighlightRegions
        {
            set
            {
                if (this.regionPanels != null)
                {
                    foreach (ImageRegionPanel regionPanel in this.regionPanels)
                    {
                        regionPanel.AlwaysShowRegionHotspot = value;
                    }
                }
            }
        }

        public Uri ImageUri
        {
            get
            {
                return this.imageUri;
            }
        }

        public double ImageWidth
        {
            get
            {
                return this.imageWidth;
            }
        }

        public double ImageHeight
        {
            get
            {
                return this.imageHeight;
            }
        }

        public List<ImageRegionPanel> RegionPanels
        {
            get
            {
                return this.regionPanels;
            }
        }

        public void SetImageParams(Uri imageUri)
        {
            // Save
            this.imageUri = imageUri;

            // Draw the Image
            ImageSource imageSource = new BitmapImage(imageUri);

            this.photoImage.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(this.ImageFailedToLoad);
            this.photoImage.Stretch = Stretch.Fill;
            this.photoImage.VerticalAlignment = VerticalAlignment.Top;
            this.photoImage.HorizontalAlignment = HorizontalAlignment.Left;
            this.photoImage.SetValue(Image.SourceProperty, imageSource);
            this.photoImage.Width = this.imageWidth;
            this.photoImage.Height = this.imageHeight;
        }

        public void SetRegionParams(MicrosoftImageRegion[] regions)
        {
            this.regionPanels = new List<ImageRegionPanel>();

            if (regions.Length > 0)
            {
                foreach (MicrosoftImageRegion region in regions)
                {
                    ImageRegionPanel regionPanel = new ImageRegionPanel(region, this.imageWidth, this.imageHeight);

                    Canvas.SetLeft(regionPanel, regionPanel.Left);
                    Canvas.SetTop(regionPanel, regionPanel.Top);

                    this.imageCanvas.Children.Add(regionPanel);

                    this.regionPanels.Add(regionPanel);
                }
            }
        }

        private void ImageFailedToLoad(object sender, ExceptionRoutedEventArgs e)
        {
            // Handle the error here
        }
    }
}
